<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/10
 * Time: 16:17
 */

namespace App\HttpController\V1;


use Co\Http\Request;
use App\HttpController\Taoran\Functions\Publics;
class Notify extends BaseWithDb
{
    /**
     * 支付宝回调商品购买
     * @param Request $request
     */
    function ali_order()
    {
        $notifiedData = $this->input();
        file_put_contents('notify.txt','支付宝商品购买'.json_encode($notifiedData).PHP_EOL,FILE_APPEND);
        $publics = new Publics();
        if($notifiedData['out_trade_no']){
            $order_info=$this->db->where('order_num',$notifiedData['out_trade_no'])->getOne('orders');
            $goods_id=$this->db->where('order_id',$order_info['id'])->getValue('orderinfo','goods_id');
            if($notifiedData['trade_status']=='TRADE_SUCCESS'&&$order_info['status']==1){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('orders',['payment_time'=>date('Y-m-d H:i:s'),'pay_style'=>1,'status'=>3]);
                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['total_money'],2,$this->db,$goods_id);
                    $group=$publics->group($order_info['user_id'],$order_info['total_money'],2,$this->db);
                    if(!$insert||!$group){
                        $this->db->rollback();
                    }
                    if($order_info['shop_car_id']){
                        $car_id=explode(",",$order_info['shop_car_id']);
                        $this->db->whereIn('id',$car_id)->delete('shop_car');
                    }
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo 'success';
    }

    /**
     * 续费会员支付宝回调
     */
    function ali_vip(){
        $notifiedData = $this->input();
        file_put_contents('notify.txt','支付宝续费会员'.json_encode($notifiedData).PHP_EOL,FILE_APPEND);
        $publics = new Publics();
        if($notifiedData['out_trade_no']){
            $order_info=$this->db->where('order_num',$notifiedData['out_trade_no'])->getOne('vip_order');
            if($notifiedData['trade_status']=='TRADE_SUCCESS'&&$order_info['status']==0){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('vip_order',['status'=>1]);
                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['money'],1,$this->db);
                    $group=$publics->group($order_info['user_id'],$order_info['money'],1,$this->db);
                    if(!$insert||!$group){
                        $this->db->rollback();
                    }
                    $user_info=$this->db->where('id',$order_info['user_id'])->getOne('users','level,vip_expire_time');
                    $num=$order_info['num']*12;
                    if($user_info['level']==1){
                        $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month"));
                    }else{
                        $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month",strtotime($user_info['vip_expire_time'])));
                    }
                    file_put_contents('notify.txt','支付宝续费会员'.$num.'==='.$order_info['user_id'].'====='.$vip_expire_time.PHP_EOL,FILE_APPEND);
                    $this->db->where('id',$order_info['user_id'])->update('users',['level'=>2,'vip_expire_time'=>$vip_expire_time]);
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo 'success';
    }

    /**
     * 支付宝支付活动
     */
    function ali_activity(){
        $notifiedData = $this->input();
        file_put_contents('notify.txt','支付宝报名活动'.json_encode($notifiedData).PHP_EOL,FILE_APPEND);
        $publics = new Publics();
        if($notifiedData['out_trade_no']){
            $order_info=$this->db->where('order_num',$notifiedData['out_trade_no'])->getOne('activitys');
            if($notifiedData['trade_status']=='TRADE_SUCCESS'&&$order_info['status']==0){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('activitys',['status'=>1]);
                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['money'],3,$this->db);
                    if(!$insert){
                        $this->db->rollback();
                    }
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo 'success';
    }

    /**
     * 微信回调购买商品
     */
    function wx_order(){
        $request =$this->request();
        $content = $request->getBody()->__toString();
        $arr = json_decode(json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        file_put_contents('notify.txt','微信购买商品'.json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)).PHP_EOL,FILE_APPEND);

        $publics = new Publics();
        if($arr['out_trade_no']){
            $order_info=$this->db->where('order_num',$arr['out_trade_no'])->getOne('orders');
            $goods_id=$this->db->where('order_id',$order_info['id'])->getValue('orderinfo','goods_id');
            if($arr['return_code']=='SUCCESS'&&$order_info['status']==1){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('orders',['payment_time'=>date('Y-m-d H:i:s'),'pay_style'=>2,'status'=>3]);

                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['total_money'],2,$this->db,$goods_id);
                    $group=$publics->group($order_info['user_id'],$order_info['total_money'],2,$this->db);
                    if(!$insert||!$group){
                        $this->db->rollback();
                    }
                    if($order_info['shop_car_id']){
                        $car_id=explode(",",$order_info['shop_car_id']);
                        $this->db->whereIn('id',$car_id)->delete('shop_car');
                    }
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

    /**
     * 会员续费微信回调
     */
    function wx_vip(){
        $request =$this->request();
        $content = $request->getBody()->__toString();
        $arr = json_decode(json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        file_put_contents('notify.txt','微信续费会员'.json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)).PHP_EOL,FILE_APPEND);
        $publics = new Publics();
        if($arr['out_trade_no']){
            $order_info=$this->db->where('order_num',$arr['out_trade_no'])->getOne('vip_order');
            if($arr['return_code']=='SUCCESS'&&$order_info['status']==0){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('vip_order',['status'=>1]);
                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['money'],1,$this->db);
                    $group=$publics->group($order_info['user_id'],$order_info['money'],1,$this->db);
                    if(!$insert||!$group){
                        $this->db->rollback();
                    }
                    $user_info=$this->db->where('id',$order_info['user_id'])->getOne('users','level,vip_expire_time');
                    $num=$order_info['num']*12;
                    if($user_info['level']==1){
                        $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month"));
                    }else{
                        $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month",strtotime($user_info['vip_expire_time'])));
                    }
                    $this->db->where('id',$order_info['user_id'])->update('users',['level'=>2,'vip_expire_time'=>$vip_expire_time]);
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

    /**
     * 微信支付回调活动
     */
    function wx_activity(){
        $request =$this->request();
        $content = $request->getBody()->__toString();
        $arr = json_decode(json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        file_put_contents('notify.txt','微信报名活动'.json_encode(simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA)).PHP_EOL,FILE_APPEND);
        $publics = new Publics();
        if($arr['out_trade_no']){
            $order_info=$this->db->where('order_num',$arr['out_trade_no'])->getOne('activitys');
            if($arr['return_code']=='SUCCESS'&&$order_info['status']==0){
                $this->db->startTransaction();
                try{
                    //记录入库
                    $this->db->where('id',$order_info['id'])->update('activitys',['status'=>1]);
                    $insert=$publics->call_inspiration($order_info['user_id'],$order_info['money'],3,$this->db);
                    if(!$insert){
                        $this->db->rollback();
                    }
                    $this->db->commit();
                }catch (\Exception $e){
                    $this->db->rollback();
                }
            }
        }
        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

}